package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/go-redis/redis/v8"
)

func Producer(client *redis.Client, queueName string, wg *sync.WaitGroup) {
	defer wg.Done()
	for i := 0; i < 20; i++ {
		message := fmt.Sprintf("Message %d", i)
		err := PushMessage(client, queueName, message)
		if err != nil {
			fmt.Printf("Failed to push message: %v\n", err)
		}
		time.Sleep(500 * time.Millisecond)
	}
}

func Consumer(client *redis.Client, queueName string, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		messages, err := PullMessages(client, queueName, 5)
		if err != nil {
			fmt.Printf("Failed to pull messages: %v\n", err)
			time.Sleep(1 * time.Second)
			continue
		}

		if len(messages) == 0 {
			time.Sleep(1 * time.Second)
			continue
		}

		fmt.Printf("Pulled messages: %v\n", messages)
		AckMessages(client, queueName, int64(len(messages)))
	}
}
